package jgl.vajra.kingkong.widget.view.painter;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.TextUtils;

import jgl.vajra.kingkong.R;
import jgl.vajra.kingkong.widget.utils.DisplayUtils;
import jgl.android.recyclerviewrefresh.utils.DensityUtil;
import com.necer.calendar.ICalendar;
import com.necer.painter.CalendarPainter;
import com.orhanobut.logger.Logger;

import org.joda.time.LocalDate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 

public class CourseArrangePainter implements CalendarPainter {

    protected Paint mTextPaint;
    private Context mContext;
    private ICalendar mICalendar;
    private Map<LocalDate, String> mArrangeMap = new HashMap<>();
    private List<LocalDate> mArrangeList = new ArrayList<>();
    private List<LocalDate> mEndList = new ArrayList<>();
    private final Paint mSelBgPaint;//选中
    private final Paint mArrangeBgPaint;//直播安排
    private final Paint mEndBgPaint;//已结束
    private int mRectRadius;


    public CourseArrangePainter(Context mContext, ICalendar mICalendar) {
        this.mContext = mContext;
        this.mICalendar = mICalendar;
        mTextPaint = getPaint();
        mSelBgPaint = getPaint();
        mSelBgPaint.setColor(Color.parseColor("#FF9312"));

        mArrangeBgPaint = getPaint();
        mArrangeBgPaint.setStrokeWidth(2);
        mArrangeBgPaint.setColor(Color.parseColor("#4794FA"));
        mArrangeBgPaint.setStyle(Paint.Style.STROKE);

        mEndBgPaint = getPaint();
        mEndBgPaint.setColor(Color.parseColor("#EDEDED"));
        mArrangeList = new ArrayList<>();

        mArrangeMap = new HashMap<>();

    }

    @Override
    public void onDrawToday(Canvas canvas, RectF rectF,  LocalDate localDate, List< LocalDate> selectedDateList) {
        boolean isSelect = selectedDateList.contains(localDate);
       //Logger.d("CourseArrangePainter onDrawToday");
        drawSelectBg(canvas, rectF, isSelect, true);
        drawArrangeBg(canvas, rectF, localDate, true,isSelect);
        drawEndBg(canvas, rectF, localDate, true,isSelect);
        drawSolar(canvas, rectF, localDate, isSelect, true);
    }

    @Override
    public void onDrawCurrentMonthOrWeek(Canvas canvas, RectF rectF,  LocalDate localDate, List< LocalDate> selectedDateList) {
      //  java.util.logging.Logger.d("CourseArrangePainter onDrawCurrentMonthOrWeek");
        boolean isSelect = selectedDateList.contains(localDate);
        drawSelectBg(canvas, rectF, isSelect, false);
        drawArrangeBg(canvas, rectF, localDate,isSelect, true);
        drawEndBg(canvas, rectF, localDate, isSelect,true);
        drawSolar(canvas, rectF, localDate, isSelect, true);
    }

    @Override
    public void onDrawLastOrNextMonth(Canvas canvas, RectF rectF,  LocalDate localDate, List< LocalDate> selectedDateList) {
       // java.util.logging.Logger.d("CourseArrangePainter onDrawLastOrNextMonth");
        boolean isSelect = selectedDateList.contains(localDate);
        drawSelectBg(canvas, rectF, isSelect, false);
        drawArrangeBg(canvas, rectF, localDate, true,isSelect);
        drawEndBg(canvas, rectF, localDate, true,isSelect);
        drawSolar(canvas, rectF, localDate, isSelect, false);
    }

    private Paint getPaint() {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextAlign(Paint.Align.CENTER);
        return paint;
    }

    //绘制直播课程背景
    private void drawArrangeBg(Canvas canvas, RectF rectF,  LocalDate date, boolean isCurrectMonthOrWeek, boolean isSelect) {
        if (!isSelect && mArrangeList.contains(date)) {
          Logger.d("drawBg ==drawArrangeBg"  );
            handlerRectF(rectF);
            canvas.drawRoundRect(rectF, DisplayUtils.getDimension(R.dimen.dp_4), DisplayUtils.getDimension(R.dimen.dp_4), mArrangeBgPaint);
        }
    }

    //绘制结束直播背景
    private void drawEndBg(Canvas canvas, RectF rectF, LocalDate date, boolean isCurrectMonthOrWeek, boolean isSelect) {
        if (!isSelect && mEndList.contains(date)) {
           Logger.d("drawBg ==drawEndBg"  );
            handlerRectF(rectF);
            canvas.drawRoundRect(rectF, DisplayUtils.getDimension(R.dimen.dp_4), DisplayUtils.getDimension(R.dimen.dp_4), mEndBgPaint);
        }
    }
    //绘制选中背景
    private void drawSelectBg(Canvas canvas, RectF rectF, boolean isisSelected, boolean isCurrectMonthOrWeek) {
        if (isisSelected) {
            Logger.d("drawBg ==drawSelectBg"  );
            handlerRectF(rectF);
            // canvas.drawRect(rectF.left + 10, rectF.top + 10, rectF.right - 10, rectF.bottom - 10, mSelBgPaint);
            canvas.drawRoundRect(rectF, DisplayUtils.getDimension(R.dimen.dp_4), DisplayUtils.getDimension(R.dimen.dp_4), mSelBgPaint);
        }
    }

    private void handlerRectF(RectF rectF) {
        rectF.left = rectF.left + 10;
        rectF.top = rectF.top + 10;
        rectF.right = rectF.right - 10;
        rectF.bottom = rectF.bottom - 10;
    }



    //绘制公历
    private void drawSolar(Canvas canvas, RectF rectF,  LocalDate localDate, boolean isSelected, boolean isCurrectMonthOrWeek) {
        mTextPaint.setTextSize(DensityUtil.dp2px(mContext, 14));
        mTextPaint.setColor(isSelected ? Color.WHITE : Color.BLACK);
        mTextPaint.setAlpha(isCurrectMonthOrWeek ? 255 : 100);
        canvas.drawText(localDate.getDayOfMonth() + "", rectF.centerX(), TextUtils.isEmpty(mArrangeMap.get(localDate)) ? getBaseLineY(rectF) : rectF.centerY(), mTextPaint);
    }

    //canvas.drawText的基准线
    private int getBaseLineY(RectF rectF) {
        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        float top = fontMetrics.top;
        float bottom = fontMetrics.bottom;
        int baseLineY = (int) (rectF.centerY() - top / 2 - bottom / 2);
        return baseLineY;
    }

    public void setArrangeMap(Map<LocalDate, String> arrangeMap) {
        if (arrangeMap != null) {
            mArrangeMap.clear();
            mArrangeMap.putAll(arrangeMap);
            mICalendar.notifyCalendar();
        }
    }

    //设置标记
    public void setTagList(List<String> list) {
        mArrangeList.clear();
        for (int i = 0; i < list.size(); i++) {
            LocalDate localDate = null;
            try {
                localDate = new LocalDate(list.get(i));
            } catch (Exception e) {
                throw new RuntimeException("setPointList的参数需要 yyyy-MM-dd 格式的日期");
            }
            mArrangeList.add(localDate);
        }
        mICalendar.notifyCalendar();
    }

    public void addArrangeList(List<String> list) {
        for (int i = 0; i < list.size(); i++) {
            LocalDate localDate = null;
            try {
                localDate = new LocalDate(list.get(i));
            } catch (Exception e) {
                throw new RuntimeException("addArrangeList的参数需要 yyyy-MM-dd 格式的日期");
            }
            if (!mArrangeList.contains(localDate)) {
                mArrangeList.add(localDate);
            }
        }
        mICalendar.notifyCalendar();
    }

    public void addEndList(List<String> list) {
        for (int i = 0; i < list.size(); i++) {
            LocalDate localDate = null;
            try {
                localDate = new LocalDate(list.get(i));
            } catch (Exception e) {
                throw new RuntimeException("addEndList的参数需要 yyyy-MM-dd 格式的日期");
            }
            if (!mEndList.contains(localDate)) {
                mEndList.add(localDate);
            }
        }
        mICalendar.notifyCalendar();
    }

 
}
